package com.chimmhuang.run;

import com.aspose.words.SaveFormat;
import com.chimmhuang.pojo.SalaryData;
import com.chimmhuang.pojo.Worker;
import com.chimmhuang.util.ContractUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Chimm Huang
 */
public class App {

    public static void main(String[] args) throws Exception {

        // 封装员工薪资信息
        Worker worker1 = Worker.builder()
                .name("张三")
                .age(18)
                .sex("男")
                .salary(BigDecimal.valueOf(1000))
                .build();
        Worker worker2 = Worker.builder()
                .name("李四")
                .age(16)
                .sex("女")
                .salary(BigDecimal.valueOf(2000))
                .build();
        List<Worker> workerList = Arrays.asList(worker1, worker2);

        // 计算工资总和
        BigDecimal totalSalary = workerList.stream()
                .map(Worker::getSalary)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        BufferedImage image = ImageIO.read(new FileInputStream("resources\\鱼.png"));
        BufferedImage bufferedImage = ContractUtil.resizeBufferedImage(image, 50, 50, true);

        // 封装文档模板数据
        SalaryData data = SalaryData.builder()
                .companyName("王多鱼股份有限公司")
                .workerList(workerList)
                .totalSalary(totalSalary)
                .logImg(bufferedImage)
                .build();


        // 读取模板文件
        Map<String, Object> dataMap = ContractUtil.getStringObjectMap(data);
        String templatePath = "resources/模板.docx";
        String docxPath = "resources/员工薪资统计表.docx";
        String docPath = "resources/员工薪资统计表.doc";
        String pdfPath = "resources/员工薪资统计表.pdf";
        ContractUtil.fillWordDataByMap(templatePath, docxPath, dataMap, SaveFormat.DOCX);
        ContractUtil.fillWordDataByMap(templatePath, docPath, dataMap, SaveFormat.DOC);
        ContractUtil.fillWordDataByMap(templatePath, pdfPath, dataMap, SaveFormat.PDF);
    }
}
